package cn.com.controller;

import cn.com.beans.CollegeInfo;
import cn.com.service.CollegeInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/insertSch")
public class InsertSchContorller {

    @Resource
    private CollegeInfoService collegeInfoService;

    @GetMapping
    public String home(){
        return "Insert/InsertColl";
    }

    @PostMapping
    @ResponseBody
    public Map<String,Object> insert(
            CollegeInfo collegeInfo, Integer coIid, String coName, String coIntroduction,
            String coCity, String coCategory, String coLevel, String coFeatures,
            String coType, String coWebsite, String coBatch, Integer coRanking,
            Integer coDoctor, Integer coMaster, String coPrid,
            @RequestParam("file") MultipartFile file){
        Map<String,Object> result = new HashMap<>();
        if(file.isEmpty()){
            System.out.println("4");
            result.put("code",1);
            result.put("msg","文件为空，新增失败");
            return result;
        }else {
            String filename = file.getOriginalFilename();
            String filepath = "D:/IdeaWork/ExamWin/src/main/resources/static/files/images/";
            File dest = new File(filepath+filename);
            try {
                file.transferTo(dest);
                collegeInfo.setCoIid(coIid);
                collegeInfo.setCoName(coName);
                collegeInfo.setCoIntroduction(coIntroduction);
                collegeInfo.setCoCity(coCity);
                collegeInfo.setCoFeatures(coFeatures);
                collegeInfo.setCoCategory(coCategory);
                collegeInfo.setCoLevel(coLevel);
                collegeInfo.setCoWebsite(coWebsite);
                collegeInfo.setCoBatch(coBatch);
                collegeInfo.setCoRanking(coRanking);
                collegeInfo.setCoDoctor(coDoctor);
                collegeInfo.setCoMaster(coMaster);
                collegeInfo.setCoType(coType);
                collegeInfo.setCoPrid(coPrid);
                System.out.println(collegeInfo);
                if (this.collegeInfoService.insert(collegeInfo)) {
                    System.out.println("1");
                    result.put("code",0);
                    result.put("msg","新增成功");
                } else {
                    System.out.println("2");
                    result.put("code",1);
                    result.put("msg","新增失败");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("3");
            return result;
        }
    }
}
